#pragma once

#include "rotor-light/definitions.hpp"
#include <stm32f407xx.h>

extern "C" {
void exti1_irq_handler_backend();
void dma2_stream6_handler_backend();
}

namespace hw {

namespace rl = rotor_light;

struct Board {
  static void init();

  static inline void toggle_led() { GPIOD->ODR ^= GPIO_ODR_OD12; }

  static rl::TimePoint get_now();

  template <int value> static inline void sensor_pin_out() {
    if constexpr (value) {
      GPIOD->ODR |= GPIO_ODR_OD1; // 1 on output
    } else {
      GPIOD->ODR &= ~GPIO_ODR_OD1; // 0 on output
    }
  }

  static inline uint32_t sensor_value() { return GPIOD->IDR & GPIO_ODR_OD1; }

  static void enable_uart();
  static void uart_write(const void *ptr, uint32_t sz);
  static void sensor_pin_input();
  static void sensor_pin_output(int value);
};

} // namespace hw
